package com.jsh.erp.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jsh.erp.datasource.vo.LogVo4List;
import com.jsh.erp.service.syslog.SysLogService;
import com.jsh.erp.utils.BaseResponseInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/sysLog")
public class SysLogController {
    @Resource
    private SysLogService sysLogService;

    @GetMapping("/list/{number}/{status}")
    public BaseResponseInfo getLogList(@PathVariable("number") Long number, @PathVariable("status") Integer status) throws Exception {
        BaseResponseInfo info = new BaseResponseInfo();
        List<LogVo4List> list = sysLogService.select(null, null, null, status, null, null, null, number, 0, 100);
        for(LogVo4List log : list) {
            if (status == 0) {
                String c = "";
                String content  = log.getContent();
                JSONArray array = JSONObject.parseArray(content);
                for (int i = 0; i < array.size(); i++) {
                    JSONObject object = array.getJSONObject(i);
                    String barCode = object.getString("barCode");
                    String name = object.getString("name");
                    Integer unitPrice = object.getInteger("unitPrice");
                    Integer finalPrice = object.getInteger("finalPrice");
                    Integer allPrice = object.getInteger("allPrice");
                    Integer operNumber = object.getInteger("operNumber");
                    String row = barCode + " " + name + "单价为：" + unitPrice + "，最终售价为：" + finalPrice + "，数量为：" + operNumber + "，合计：" + allPrice + "元" + "\n";
                    c += row;
                }
                log.setContent(c);
            }
        }
        info.data = list;
        info.code = 200;
        return info;
    }
}
